package com.gx.smart.lib.http.api;

import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.asynctask.UAAGRPCAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.gx.wisestone.uaa.grpc.lib.auth.AuthApiGrpc;
import com.gx.wisestone.uaa.grpc.lib.auth.LoginReq;
import com.gx.wisestone.uaa.grpc.lib.auth.LoginResp;
import com.gx.wisestone.uaa.grpc.lib.auth.UserModifyReq;
import com.gx.wisestone.uaa.grpc.lib.auth.UserModifyResp;
import com.gx.wisestone.uaa.grpc.lib.auth.VerifyCodeReq;
import com.gx.wisestone.uaa.grpc.lib.auth.VerifyCodeResp;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author xiaosy
 * @create 2020/8/11
 * @Describe 获取授权
 **/
public class AuthApiService {
    private String host = BuildConfig.API_FORMAL ? ApiConfig.JoyLife_gRPC_Server_1_FORMAL :
            BuildConfig.JOYLIFE_GRPC_SERIVER_URL;
    private String port = "41772";
    private String user_space = ApiConfig.HUISHI_USER_SPACE_FORMAL;

    private AuthApiService() {

    }

    public static AuthApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private AuthApiGrpc.AuthApiBlockingStub getStub(ManagedChannel channel) {
        return AuthApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 登陆
     *
     * @param callBack
     * @return
     */
    public UAAGRPCAsyncTask<String, Void, LoginResp> login(
            final String account,
            final String password,
            final int login_type,
            final CallBack<LoginResp> callBack) {
        return new UAAGRPCAsyncTask<String, Void, LoginResp>(callBack) {
            @Override
            protected LoginResp doRequestData(ManagedChannel channel) {
                LoginReq message = LoginReq.newBuilder()
                        .setUserSpace(user_space)
                        .setAccount(account)
                        .setPassword(password)
                        .setLoginType(login_type)
                        .build();
                Logger.d(message);
                LoginResp response = null;
                try {
                    response = getStub(channel).login(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 验证码
     * * @param target     验证码 目标
     * * @param targetType 目标类型 1 手机号; 2 邮箱; 3 微信;
     * * @param purpose    用途 1 登录 2 注册 3 修改
     * * @return callBack返回值
     *
     * @param callBack
     * @return
     */
    public UAAGRPCAsyncTask<String, Void, VerifyCodeResp> verifyCode(
            final String target,
            final int targetType,
            final int purpose,
            CallBack callBack) {
        return new UAAGRPCAsyncTask<String, Void, VerifyCodeResp>(callBack) {
            @Override
            protected VerifyCodeResp doRequestData(ManagedChannel channel) {
                VerifyCodeReq message = VerifyCodeReq.newBuilder()
                        .setUserSpace(user_space)
                        .setTarget(target)
                        .setTargetType(targetType)
                        .setPurpose(purpose)
                        .build();
                Logger.d(message);
                VerifyCodeResp response = null;
                try {
                    response = getStub(channel).verifyCode(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "verifyCode");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 用户密码修改
     *
     * @param password (必) 密码/验证码 根据登录类型而定
     * @return callBack返回值
     */
    public UAAGRPCAsyncTask<String, Void, UserModifyResp> userModifyPassWord(final String password, final String userId, final String token, CallBack callBack) {
        return new UAAGRPCAsyncTask<String, Void, UserModifyResp>(callBack) {
            @Override
            protected UserModifyResp doRequestData(ManagedChannel channel) {
                UserModifyReq message = UserModifyReq.newBuilder()
                        .setUserSpace(user_space)
                        .setPassword(password)
                        .setUserId(userId)
                        .setToken(token)
                        .build();
                Logger.d(message);
                UserModifyResp response = null;
                try {
                    response = getStub(channel).userModify(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "AuthApi_gRPC");
                }

                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    private static class SingleTonHolder {
        private static final AuthApiService INSTANCE = new AuthApiService();
    }
}
